package com.sinosoft.controller;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sinosoft.base.BaseFormController;
import com.sinosoft.base.Constants;
import com.sinosoft.product.AdviceProductCondition;
import com.sinosoft.product.ProductCondition;
import com.sinosoft.product.ProductSearchManager;
import com.sinosoft.util.DateTimeUtils;
import com.sinosoft.util.SystemConfigUtil;

@Controller
public class ProductSearchController extends BaseFormController {
	
	ProductSearchManager productManager;
	
	@Autowired
	public void setProductSearchManager(ProductSearchManager pManager){
		this.productManager = pManager;
	}
	
	
	
	@RequestMapping("/toCollectPage.html")
	public String toColectionPage(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		String sensors = request.getParameter("sensors");
		String minCC = request.getParameter("minCC");
		String maxCC = request.getParameter("maxCC");
		HashMap information = new HashMap();
		information.put("satAndSensors", sensors);
		information.put("maxCC", maxCC);
		
		request.getSession().setAttribute("productToCollectinfo", information);
		
		response.setContentType("applicate/json");
        PrintWriter out = response.getWriter();
        out.write("{\"handleStatue\":true}");
        out.flush();
        out.close();
		return null;
	}
	
	@RequestMapping("/productSearch.html")
	public String toProductSearchPage(Model model,HttpServletRequest request,HttpServletResponse response)
	{
		String startDate = DateTimeUtils.DateToString(DateTimeUtils.addDays(DateTimeUtils.getNowDate(), -7), DateTimeUtils.JAVA_DATE_FORMATTER);
		String endDate = DateTimeUtils.DateToString(DateTimeUtils.getNowDate(), DateTimeUtils.JAVA_DATE_FORMATTER);
		request.setAttribute("startDate", startDate);
		request.setAttribute("endDate", endDate);
		
		return "zywxmap/zywxmap";
	}
	
	@RequestMapping("/specialSearch.html")
	public String toSpecialSearchPage(Model model,HttpServletRequest request,HttpServletResponse response)
	{
		String startDate = DateTimeUtils.DateToString(DateTimeUtils.addDays(DateTimeUtils.getNowDate(), -7), DateTimeUtils.JAVA_DATE_FORMATTER);
		String endDate = DateTimeUtils.DateToString(DateTimeUtils.getNowDate(), DateTimeUtils.JAVA_DATE_FORMATTER);
		request.setAttribute("startDate", startDate);
		request.setAttribute("endDate", endDate);
		return "product/specialSearch";
	}
	
	@RequestMapping("/doSpecialSearch.html")
	protected ModelAndView doSpecialSearch(HttpServletRequest request,HttpServletResponse response) throws Exception {
		HashMap<String, String> queryParams = new HashMap<String, String>();    
		
		String productId = request.getParameter("productId");
		queryParams.put("productId", productId);
		String specialType = request.getParameter("sType");
		queryParams.put("sType", specialType);
		String prodstartdate = request.getParameter("prodStartDate");
		queryParams.put("prodStartDate", prodstartdate);
		String proenddate = request.getParameter("prodEndDate");
		queryParams.put("prodEndDate", proenddate);
        
        String results = productManager.doSpecialSearch(queryParams);
      
//	    System.out.println(results);
        response.setContentType("applicate/json");
        PrintWriter out = response.getWriter();
        out.write(results);
        out.flush();
        out.close();
        return null;
    }
	
	@RequestMapping("/doProductSearch.html")
	protected ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {
		HashMap<String, String> queryParams = new HashMap<String, String>();    
		
		String productLevel = request.getParameter("productLevel");
		queryParams.put("productLevel", productLevel);
		String sensors = request.getParameter("sensors");
		queryParams.put("sensors", sensors);
		String stations = request.getParameter("stations");
		queryParams.put("stations", stations);
		String colStartDate = request.getParameter("colStartDate");
		queryParams.put("colStartDate", colStartDate);
		String colEndDate = request.getParameter("colEndDate");
		queryParams.put("colEndDate", colEndDate);
		String prodStartDate = request.getParameter("prodStartDate");
		queryParams.put("prodStartDate", prodStartDate);
		String prodEndDate = request.getParameter("prodEndDate");
		queryParams.put("prodEndDate", prodEndDate);
		String minCC = request.getParameter("minCC");
		queryParams.put("minCC", minCC);
		String maxCC = request.getParameter("maxCC");
		queryParams.put("maxCC", maxCC);
        
        String results = productManager.doProductSearch(queryParams);
      
//	        System.out.println(results);
        response.setContentType("applicate/json");
        PrintWriter out = response.getWriter();
        out.write(results);
        out.flush();
        out.close();
        return null;
    }
	
	/**
	 * 浜у搧杩囨护
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/productfilter.html")
	public String ProductFilter(HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		String results = null;
		String ids = request.getParameter("productid");
		String tablename = request.getParameter("tablename");
		String ordername = request.getParameter("ordername");
		results = productManager.ProductFilterManager(ids, ordername, tablename);
		response.setContentType("applicate/json");
        PrintWriter out = response.getWriter();
        out.write(results);
        out.flush();
        out.close();
		return null;
	}
	
	/**
	 * 閲嶆柊鏌ヨ
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/contiuneSearch.html")
	public String contiuneSearch(HttpServletRequest request,HttpServletResponse response)
	{
		Object objcon = request.getSession().getAttribute(Constants.MAP_DATA_CONDITION);
		ProductCondition con = null;
		if(objcon!=null)
		{
			con = (ProductCondition)objcon;
			request.getSession().removeAttribute(Constants.MAP_DATA_CONDITION);
		}
		else
		{
			con = new ProductCondition();
		}
		con.setOperate(new BigDecimal("0"));//閲嶆柊璁㈣喘
		request.getSession().setAttribute(Constants.MAP_DATA_CONDITION, con);
		//鍒ゆ柇璺宠浆鏈�悗涓�鐨勬煡璇㈡柟寮�/
		Object obj = request.getSession().getAttribute(Constants.MAP_DATA_OPERATE);
		if(obj!=null)
		{
			if(obj.toString().equals("1"))//浜у搧鏌ヨ
			{
				return "redirect:/zywxmap/zywxmap.htm";
			}
			else if(obj.toString().equals("2"))//楂樼骇鏌ヨ
			{
				return "redirect:/zywxmap/AdvanceSearch.htm";
			}
			else//涓撻鏌ヨ
			{
				
			}
		}
		return "redirect:/zywxmap/zywxmap.htm";
	}
	
	/**
	 * 閲嶆柊璁㈣喘
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/contiuneorder.html")
	public String contiuneorder(HttpServletRequest request,HttpServletResponse response)
	{
		Object obj = request.getSession().getAttribute(Constants.MAP_DATA_CONDITION);
		ProductCondition con = null;
		if(obj!=null)
		{
			con = (ProductCondition)obj;
			request.getSession().removeAttribute(Constants.MAP_DATA_CONDITION);
		}
		else
		{
			con = new ProductCondition();
		}
		con.setOperate(new BigDecimal("1"));//閲嶆柊璁㈣喘
		request.getSession().setAttribute(Constants.MAP_DATA_CONDITION, con);
		//鍒ゆ柇璺宠浆鏈�悗涓�鐨勬煡璇㈡柟寮�/
		Object objope = request.getSession().getAttribute(Constants.MAP_DATA_OPERATE);
		if(objope!=null)
		{
			if(objope.toString().equals("1"))//浜у搧鏌ヨ
			{
				return "redirect:/zywxmap/zywxmap.htm";
			}
			else if(objope.toString().equals("2"))//楂樼骇鏌ヨ
			{
				return "redirect:/zywxmap/AdvanceSearch.htm";
			}
			else//涓撻鏌ヨ
			{
				
			}
		}
		return "redirect:/zywxmap/zywxmap.htm";
	}
	
	/**
	 * 淇濆瓨浜у搧鏌ヨ鏉′欢
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/setproductsession.html")
	public String setProductSession(HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		String results = "success";
		String leftupperlong = request.getParameter("leftupperlong");
		String leftupperlat = request.getParameter("leftupperlat");
		String rightdownlong = request.getParameter("rightdownlong");
		String rightdownlat = request.getParameter("rightdownlat");
		String imagegsd = request.getParameter("imagegsd");
		String scenestartdate = request.getParameter("scenestartdate");
		String sceneenddate = request.getParameter("sceneenddate");
		String satesensor = request.getParameter("satesensor");//sate:sensor1,sensor2
		String pathrow = request.getParameter("pathrow");//sate:minpath-maxpath,minrow-maxrow
		String level = request.getParameter("level");
		String productstartdate = request.getParameter("productstartdate");
		String productenddate = request.getParameter("productenddate");
		String receivestationid = request.getParameter("receivestationid");
		String mincloudpercent = request.getParameter("mincloudpercent");
		String maxcloudpercent = request.getParameter("maxcloudpercent");
		String overalldataquality = request.getParameter("overalldataquality");
		String sql = request.getParameter("sql");
		
		ProductCondition condition = new ProductCondition();
		if(leftupperlong!=null && !leftupperlong.isEmpty())
			condition.setLeftupperlong(new BigDecimal(leftupperlong));
		if(leftupperlat!=null && !leftupperlat.isEmpty())
			condition.setLeftupperlat(new BigDecimal(leftupperlat));
		if(rightdownlat!=null && !rightdownlat.isEmpty())
			condition.setRightdownlat(new BigDecimal(rightdownlat));
		if(rightdownlong!=null && !rightdownlong.isEmpty())
			condition.setRightdownlong(new BigDecimal(rightdownlong));
		if(imagegsd!=null && !imagegsd.isEmpty())
			condition.setImagegsd(new BigDecimal(imagegsd));
		if(scenestartdate!=null && !scenestartdate.isEmpty())
			condition.setScenestartdate(scenestartdate);
		if(sceneenddate!=null && !sceneenddate.isEmpty())
			condition.setSceneenddate(sceneenddate);
		if(level!=null && !level.isEmpty())
			condition.setLevel(new BigDecimal(level));
			
		condition.setSatesensor(satesensor);
		condition.setPathrow(pathrow);
		
		
		if(productstartdate!=null && !productstartdate.isEmpty())
			condition.setProductstartdate(productstartdate);
		if(productenddate!=null && !productenddate.isEmpty())
			condition.setProductenddate(productenddate);
		
		condition.setReceivestationid(receivestationid);
		
		if(mincloudpercent!=null && !mincloudpercent.isEmpty())
			condition.setMincloudpercent(new BigDecimal(mincloudpercent));
		if(maxcloudpercent!=null && !maxcloudpercent.isEmpty())
			condition.setMaxcloudpercent(new BigDecimal(maxcloudpercent));
		if(overalldataquality!=null && !overalldataquality.isEmpty())
			condition.setOveralldataquality(new BigDecimal(overalldataquality));
		
		condition.setSqlstr(sql);
		request.getSession().setAttribute(Constants.MAP_DATA_CONDITION, condition);
		request.getSession().setAttribute(Constants.MAP_DATA_OPERATE, "1");
		response.setContentType("applicate/json");
        PrintWriter out = response.getWriter();
        out.write(results);
        out.flush();
        out.close();
		return null;
	}
	
	/**
	 * 璇诲彇浜у搧鏌ヨ鏉′欢
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/getproductsession.html")
	public String getProductSession(HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		ProductCondition condition1 = new ProductCondition();
//		娴嬭瘯寮�
		condition1.setLeftupperlong(new BigDecimal("71.42930578070528"));
		condition1.setLeftupperlat(new BigDecimal("57.38249207126553"));
		condition1.setRightdownlat(new BigDecimal("147.43958841670533"));
		condition1.setRightdownlong(new BigDecimal("15.98403456415835"));
//		condition1.setImagegsd(new BigDecimal("15"));
//		condition1.setScenestartdate(DateTimeUtils.DateToString(new Date(), DateTimeUtils.JAVA_DATE_FORMATTER));
//		condition1.setSceneenddate(DateTimeUtils.DateToString(new Date(), DateTimeUtils.JAVA_DATE_FORMATTER));
		condition1.setSatesensor("ZY3_NAD@ZY3_BWD@ZY3_FWD@ZY3_MUX@ZY3_TLC");
		condition1.setLevel(new BigDecimal("6"));
		condition1.setReceivestationid("MYN,URQ,GUA,KAS,SAY");
//		condition1.setProductstartdate(DateTimeUtils.DateToString(new Date(), DateTimeUtils.JAVA_DATE_FORMATTER));
//		condition1.setProductenddate(DateTimeUtils.DateToString(new Date(), DateTimeUtils.JAVA_DATE_FORMATTER));
//		condition1.setReceivestationid("2");
//		condition1.setMincloudpercent(new BigDecimal("0"));
//		condition1.setMaxcloudpercent(new BigDecimal("100"));
//		condition1.setOveralldataquality(new BigDecimal("2"));
		condition1.setOperate(new BigDecimal("1"));//0:閲嶆柊鏌ヨ 1:閲嶆柊璁㈣喘
		request.getSession().setAttribute("condition", condition1);
//		娴嬭瘯缁撴潫
		
		Object obj = request.getSession().getAttribute(Constants.MAP_DATA_CONDITION);
		JSONObject jsonObj = new JSONObject();
		String result = "";
		if(obj!=null)
		{
			ProductCondition condition = (ProductCondition)obj;
			if(condition!=null&&condition.getOperate()!=null)
			{
//				jsonObj.put("sql", condition.getSqlstr());
				jsonObj.put("leftupperlong",condition.getLeftupperlong());
				jsonObj.put("leftupperlat",condition.getLeftupperlat());
				jsonObj.put("rightdownlong",condition.getRightdownlong());
				jsonObj.put("rightdownlat",condition.getRightdownlat());
				jsonObj.put("imagegsd",condition.getImagegsd());
				jsonObj.put("scenestartdate",condition.getScenestartdate());
				jsonObj.put("sceneenddate",condition.getSceneenddate());
				jsonObj.put("satesensor",condition.getSatesensor());
				jsonObj.put("pathrow", condition.getPathrow());
				jsonObj.put("level",condition.getLevel());
				jsonObj.put("productstartdate",condition.getProductstartdate());
				jsonObj.put("productenddate",condition.getProductenddate());
				jsonObj.put("receivestationid",condition.getReceivestationid());
				jsonObj.put("mincloudpercent",condition.getMincloudpercent());
				jsonObj.put("maxcloudpercent",condition.getMaxcloudpercent());
				jsonObj.put("overalldataquality",condition.getOveralldataquality());
				jsonObj.put("operate",condition.getOperate());
				result = jsonObj.toString();
				condition.setOperate(null);
				request.getSession().removeAttribute(Constants.MAP_DATA_CONDITION);
				request.getSession().setAttribute(Constants.MAP_DATA_CONDITION,condition);
			}
		}
		response.setContentType("applicate/json");
        PrintWriter out = response.getWriter();
        out.write(result);
        out.flush();
        out.close();
		return null;
	}
	
	/**
	 * 淇濆瓨楂樼骇浜у搧鏌ヨ鏉′欢
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/setAdviceproductsession.html")
	public String setAdviceProductSession(HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		String results = "success";
		String leftupperlong = request.getParameter("leftupperlong");
		String leftupperlat = request.getParameter("leftupperlat");
		String rightdownlong = request.getParameter("rightdownlong");
		String rightdownlat = request.getParameter("rightdownlat");
		String scenestartdate = request.getParameter("scenestartdate");
		String sceneenddate = request.getParameter("sceneenddate");
		String satesensorpathrow = request.getParameter("satesensorpathrow");
		String level = request.getParameter("level");
		String productstartdate = request.getParameter("productstartdate");
		String productenddate = request.getParameter("productenddate");
		String sql = request.getParameter("sql");
		
		AdviceProductCondition condition = new AdviceProductCondition();
		if(leftupperlong!=null && !leftupperlong.isEmpty())
			condition.setLeftupperlong(new BigDecimal(leftupperlong));
		if(leftupperlat!=null && !leftupperlat.isEmpty())
			condition.setLeftupperlat(new BigDecimal(leftupperlat));
		if(rightdownlat!=null && !rightdownlat.isEmpty())
			condition.setRightdownlat(new BigDecimal(rightdownlat));
		if(rightdownlong!=null && !rightdownlong.isEmpty())
			condition.setRightdownlong(new BigDecimal(rightdownlong));
		if(scenestartdate!=null && !scenestartdate.isEmpty())
			condition.setScenestartdate(scenestartdate);
		if(sceneenddate!=null && !sceneenddate.isEmpty())
			condition.setSceneenddate(sceneenddate);
		if(sceneenddate!=null && !sceneenddate.isEmpty())
			condition.setLevel(new BigDecimal(level));
			
		condition.setSatesensor(satesensorpathrow);
		
		
		if(productstartdate!=null && !productstartdate.isEmpty())
			condition.setProductstartdate(productstartdate);
		if(productenddate!=null && !productenddate.isEmpty())
			condition.setProductenddate(productenddate);
		
		
		condition.setSqlstr(sql);
		request.getSession().setAttribute(Constants.MAP_DATA_CONDITION, condition);
		request.getSession().setAttribute(Constants.MAP_DATA_OPERATE, "2");
		response.setContentType("applicate/json");
        PrintWriter out = response.getWriter();
        out.write(results);
        out.flush();
        out.close();
		return null;
	}
	
	/**
	 * 璇诲彇楂樼骇浜у搧鏌ヨ鏉′欢
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/getAdviceproductsession.html")
	public String getAdviceProductSession(HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		AdviceProductCondition condition1 = new AdviceProductCondition();
		//娴嬭瘯寮�
//		condition1.setLeftupperlong(new BigDecimal("11"));
//		condition1.setLeftupperlat(new BigDecimal("12"));
//		condition1.setRightdownlat(new BigDecimal("13"));
//		condition1.setRightdownlong(new BigDecimal("14"));
//		condition1.setScenestartdate(DateTimeUtils.DateToString(new Date(), DateTimeUtils.JAVA_DATE_FORMATTER));
//		condition1.setSceneenddate(DateTimeUtils.DateToString(new Date(), DateTimeUtils.JAVA_DATE_FORMATTER));
//		condition1.setSatesensor("ZY3-FWD,ZY02C-HRC");
//		condition1.setLevel(new BigDecimal("5"));
//		condition1.setProductstartdate(DateTimeUtils.DateToString(new Date(), DateTimeUtils.JAVA_DATE_FORMATTER));
//		condition1.setProductenddate(DateTimeUtils.DateToString(new Date(), DateTimeUtils.JAVA_DATE_FORMATTER));
//		condition1.setOperate(new BigDecimal("1"));//0:閲嶆柊鏌ヨ 1:閲嶆柊璁㈣喘
//		request.getSession().setAttribute("condition", condition1);
		//娴嬭瘯缁撴潫
		
		Object obj = request.getSession().getAttribute(Constants.MAP_DATA_CONDITION);
		JSONObject jsonObj = new JSONObject();
		String result = "";
		if(obj!=null)
		{
			AdviceProductCondition condition = (AdviceProductCondition)obj;
			if(condition!=null&&condition.getOperate()!=null)
			{
				jsonObj.put("sql", condition.getSqlstr());
				jsonObj.put("leftupperlong",condition.getLeftupperlong());
				jsonObj.put("leftupperlat",condition.getLeftupperlat());
				jsonObj.put("rightdownlong",condition.getRightdownlong());
				jsonObj.put("rightdownlat",condition.getRightdownlat());
				jsonObj.put("scenestartdate",condition.getScenestartdate());
				jsonObj.put("sceneenddate",condition.getSceneenddate());
				jsonObj.put("satesensorpathrow",condition.getSatesensor());
				jsonObj.put("level",condition.getLevel());
				jsonObj.put("productstartdate",condition.getProductstartdate());
				jsonObj.put("productenddate",condition.getProductenddate());
				jsonObj.put("operate",condition.getOperate());
				result = jsonObj.toString();
				condition.setOperate(null);
				request.getSession().removeAttribute(Constants.MAP_DATA_CONDITION);
				request.getSession().setAttribute(Constants.MAP_DATA_CONDITION,condition);
			}
		}
		response.setContentType("applicate/json");
        PrintWriter out = response.getWriter();
        out.write(result);
        out.flush();
        out.close();
		return null;
	}
}
